from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field
from enum import Enum


class VideoPlatform(str, Enum):
    """支持的视频平台"""
    FACEBOOK = "facebook"
    INSTAGRAM = "instagram"
    TWITTER = "twitter"
    YOUTUBE = "youtube"
    UNKNOWN = "unknown"


class VideoFormat(BaseModel):
    """通用视频格式信息"""
    format_id: Optional[str] = Field(None, description="格式ID")
    ext: Optional[str] = Field(None, description="文件扩展名")
    resolution: Optional[str] = Field(None, description="分辨率")
    width: Optional[int] = Field(None, description="视频宽度")
    height: Optional[int] = Field(None, description="视频高度")
    filesize: Optional[int] = Field(None, description="文件大小（字节）")
    filesize_approx: Optional[int] = Field(None, description="估计文件大小（字节）")
    url: Optional[str] = Field(None, description="下载链接")
    quality: Optional[float] = Field(None, description="质量分数")
    fps: Optional[float] = Field(None, description="帧率")
    vcodec: Optional[str] = Field(None, description="视频编码")
    acodec: Optional[str] = Field(None, description="音频编码")
    tbr: Optional[float] = Field(None, description="总比特率")


class UniversalVideoInfo(BaseModel):
    """通用视频信息"""
    # 基本信息
    platform: VideoPlatform = Field(..., description="视频平台")
    video_id: Optional[str] = Field(None, description="视频ID")
    title: Optional[str] = Field(None, description="视频标题")
    description: Optional[str] = Field(None, description="视频描述")
    
    # 作者信息
    uploader: Optional[str] = Field(None, description="上传者名称")
    uploader_id: Optional[str] = Field(None, description="上传者ID")
    uploader_url: Optional[str] = Field(None, description="上传者链接")
    
    # 视频属性
    duration: Optional[float] = Field(None, description="视频时长（秒）")
    width: Optional[int] = Field(None, description="视频宽度")
    height: Optional[int] = Field(None, description="视频高度")
    thumbnail: Optional[str] = Field(None, description="缩略图链接")
    
    # 社交数据
    view_count: Optional[int] = Field(None, description="观看次数")
    like_count: Optional[int] = Field(None, description="点赞数")
    comment_count: Optional[int] = Field(None, description="评论数")
    share_count: Optional[int] = Field(None, description="分享数")
    
    # 时间信息
    upload_date: Optional[str] = Field(None, description="上传日期")
    timestamp: Optional[int] = Field(None, description="时间戳")
    
    # 技术信息
    webpage_url: Optional[str] = Field(None, description="网页链接")
    is_video: Optional[bool] = Field(None, description="是否为视频")
    formats: List[VideoFormat] = Field(default_factory=list, description="可用视频格式列表")


class UniversalVideoDownload(BaseModel):
    """通用视频下载信息"""
    # 基本信息
    platform: VideoPlatform = Field(..., description="视频平台")
    video_id: Optional[str] = Field(None, description="视频ID")
    title: Optional[str] = Field(None, description="视频标题")
    description: Optional[str] = Field(None, description="视频描述")
    
    # 作者信息
    uploader: Optional[str] = Field(None, description="上传者名称")
    uploader_id: Optional[str] = Field(None, description="上传者ID")
    uploader_url: Optional[str] = Field(None, description="上传者链接")
    
    # 下载信息
    download_url: str = Field(..., description="视频下载链接")
    format: Optional[str] = Field(None, description="视频格式")
    resolution: Optional[str] = Field(None, description="分辨率")
    width: Optional[int] = Field(None, description="视频宽度")
    height: Optional[int] = Field(None, description="视频高度")
    filesize: Optional[int] = Field(None, description="文件大小（字节）")
    filesize_approx: Optional[int] = Field(None, description="估计文件大小（字节）")
    
    # 视频属性
    duration: Optional[float] = Field(None, description="视频时长（秒）")
    thumbnail: Optional[str] = Field(None, description="缩略图链接")
    fps: Optional[float] = Field(None, description="帧率")
    vcodec: Optional[str] = Field(None, description="视频编码")
    acodec: Optional[str] = Field(None, description="音频编码")
    tbr: Optional[float] = Field(None, description="总比特率")
    
    # 社交数据
    view_count: Optional[int] = Field(None, description="观看次数")
    like_count: Optional[int] = Field(None, description="点赞数")
    comment_count: Optional[int] = Field(None, description="评论数")
    share_count: Optional[int] = Field(None, description="分享数")
    
    # 时间和链接信息
    timestamp: Optional[int] = Field(None, description="时间戳")
    webpage_url: Optional[str] = Field(None, description="网页链接")
    is_video: Optional[bool] = Field(None, description="是否为视频")


class UniversalQualityUrl(BaseModel):
    """通用质量链接信息"""
    url: str = Field(..., description="下载链接")
    format: Optional[str] = Field(None, description="视频格式")
    resolution: Optional[str] = Field(None, description="分辨率")
    width: Optional[int] = Field(None, description="视频宽度")
    height: Optional[int] = Field(None, description="视频高度")
    filesize: Optional[int] = Field(None, description="文件大小（字节）")
    filesize_approx: Optional[int] = Field(None, description="估计文件大小（字节）")
    fps: Optional[float] = Field(None, description="帧率")
    vcodec: Optional[str] = Field(None, description="视频编码")
    acodec: Optional[str] = Field(None, description="音频编码")
    tbr: Optional[float] = Field(None, description="总比特率")


class UniversalMultiQualityInfo(BaseModel):
    """通用多质量下载信息"""
    # 基本信息
    platform: VideoPlatform = Field(..., description="视频平台")
    video_id: Optional[str] = Field(None, description="视频ID")
    title: Optional[str] = Field(None, description="视频标题")
    description: Optional[str] = Field(None, description="视频描述")
    
    # 作者信息
    uploader: Optional[str] = Field(None, description="上传者名称")
    uploader_id: Optional[str] = Field(None, description="上传者ID")
    uploader_url: Optional[str] = Field(None, description="上传者链接")
    
    # 视频属性
    duration: Optional[float] = Field(None, description="视频时长（秒）")
    thumbnail: Optional[str] = Field(None, description="缩略图链接")
    
    # 社交数据
    view_count: Optional[int] = Field(None, description="观看次数")
    like_count: Optional[int] = Field(None, description="点赞数")
    comment_count: Optional[int] = Field(None, description="评论数")
    share_count: Optional[int] = Field(None, description="分享数")
    
    # 多质量链接
    quality_urls: Dict[str, UniversalQualityUrl] = Field(default_factory=dict, description="各质量下载链接")
    available_qualities: List[str] = Field(default_factory=list, description="可用质量列表")
    
    # 时间和链接信息
    timestamp: Optional[int] = Field(None, description="时间戳")
    webpage_url: Optional[str] = Field(None, description="网页链接")
    is_video: Optional[bool] = Field(None, description="是否为视频")


# 响应模型
class UniversalVideoInfoResponse(BaseModel):
    """通用视频信息响应"""
    success: bool = Field(..., description="是否成功")
    platform: Optional[VideoPlatform] = Field(None, description="识别的平台")
    data: Optional[UniversalVideoInfo] = Field(None, description="视频信息数据")
    error: Optional[str] = Field(None, description="错误信息")


class UniversalVideoDownloadResponse(BaseModel):
    """通用视频下载响应"""
    success: bool = Field(..., description="是否成功")
    platform: Optional[VideoPlatform] = Field(None, description="识别的平台")
    data: Optional[UniversalVideoDownload] = Field(None, description="下载信息数据")
    error: Optional[str] = Field(None, description="错误信息")


class UniversalMultiQualityResponse(BaseModel):
    """通用多质量响应"""
    success: bool = Field(..., description="是否成功")
    platform: Optional[VideoPlatform] = Field(None, description="识别的平台")
    data: Optional[UniversalMultiQualityInfo] = Field(None, description="多质量下载数据")
    error: Optional[str] = Field(None, description="错误信息")


class UniversalSupportedPlatformsResponse(BaseModel):
    """支持的平台响应"""
    success: bool = Field(True, description="是否成功")
    platforms: List[Dict[str, Any]] = Field(default_factory=list, description="支持的平台列表")
    total_platforms: int = Field(0, description="支持的平台总数")
    description: str = Field("", description="平台支持说明") 